package com.jerei.store.service.impl;

import com.jerei.store.entity.Product;
import com.jerei.store.mapper.ProductMapper;
import com.jerei.store.service.IProductService;
import com.jerei.store.service.ex.SelectException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class ProductServiceImpl implements IProductService {

    @Autowired
    private ProductMapper mapper;

    @Override
    public List<Product> findProduct() {
        List<Product> list=mapper.findProduct();
        if(list.isEmpty()){
            throw new SelectException("数据为空或者查询异常");
        }
        return list;
    }

    @Override
    public List<Product> findProductByTime() {
        List<Product> list=mapper.findProductByTime();
        if(list.isEmpty()){
            throw new SelectException("数据为空或者查询异常");
        }
        return list;
    }

    @Override
    public Product findById(Integer id) {
        System.out.println("findById id="+id);
        Product product=mapper.findById(id);
        if(product==null){
            throw new SelectException("查询失败！该产品不存在或者查询异常");
        }
        return product;
    }

    @Override
    public List<Product> findByLike(String name,Integer index) {
        if(index<=0){
            throw new SelectException("模糊查询页面下标错误");
        }
        Integer num=(index-1)*12;
        List<Product> list=mapper.findByLike(name,num);
        if(list.isEmpty()){
            throw new SelectException("查询结果为空，或者查询异常");
        }
        return list;
    }

    @Override
    public Integer findCount(String name) {
        Integer num=mapper.findByLikeCount(name);
        if(num==0){
            throw new SelectException("数量为0");
        }
        return num;
    }
}
